﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Maxleaf.EDI.Core.Handlers;
using Maxleaf.EDI.Core.Handlers.CommandHandlers;
using Maxleaf.EDI.Core.Handlers.QueryHandlers;
using Maxleaf.EDI.DataAccess.Repositorys;
using Maxleaf.EDI.DataAccess.UnitOfWork;
using Maxleaf.EDI.Domain;
using Maxleaf.EDI.Domain.EDI;
using Maxleaf.EDI.Domain.K3;
using TinyIoC;

namespace Maxleaf.EDI.Core.GlobalConfiguration
{
    public class TinyIoCConfiguration
    {
        public static readonly TinyIoCContainer Container = TinyIoCContainer.Current;

        public TinyIoCConfiguration()
        {
        }

        public void Configure()
        {
            Container.AutoRegister();

            CommandRegistration();

            QueryRegistration();

            DataAccessRegistration();
        }
        
        private static void QueryRegistration()
        {
            Container.Register<IQueryHandler<Maxleaf.EDI.API.Contract.Client.Parameters.AuthenticateParameters, Maxleaf.EDI.API.Contract.Client.Parameters.AuthenticateResult>, AuthenticateQueryHandler>();
            Container.Register<IQueryHandler<Maxleaf.EDI.API.Contract.Client.Parameters.GetUserDBsParameters, Maxleaf.EDI.API.Contract.Client.Parameters.GetUserDBsResult>, GetUserDBsQueryHandler>();

            Container.Register<IQueryHandler<Maxleaf.EDI.API.Contract.Admin.Parameters.AuthenticateParameters, Maxleaf.EDI.API.Contract.Admin.Parameters.AuthenticateResult>, AuthenticateAdminQueryHandler>();
            Container.Register<IQueryHandler<Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllUsersParameters, Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllUsersResult>, GetAllUsersQueryHandler>();
            Container.Register<IQueryHandler<Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllDBsParameters, Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllDBsResult>, GetAllDBsQueryHandler>();
        }

        private static void DataAccessRegistration()
        {
            Container.Register<IUnitOfWorkProvider, EFUnitOfWorkProvider>();

            Container.Register<IRepository<User_DB>, RepositoryBase<User_DB>>();
            Container.Register<IRepository<DBDesc>, RepositoryBase<DBDesc>>();
            Container.Register<IRepository<EX_Bill>, RepositoryBase<EX_Bill>>();

            Container.Register<IRepository<SEOrder>, RepositoryBase<SEOrder>>();
            Container.Register<IRepository<SEOrderEntry>, RepositoryBase<SEOrderEntry>>();

            Container.Register<IRepository<t_Item>, RepositoryBase<t_Item>>();
        }

        private static void CommandRegistration()
        {
            Container.Register<ICommandHandler<Maxleaf.EDI.API.Contract.K3.Commands.ImportPurchaseOrderCommand>, ImportPurchaseOrderCommandHandler>();
            Container.Register<ICommandHandler<Maxleaf.EDI.API.Contract.K3.Commands.ImportSalesOrderCommand>, ImportSalesOrderCommandHandler>();
            Container.Register<ICommandHandler<Maxleaf.EDI.API.Contract.Admin.Commands.CreateUserCommand>, CreateUserCommandHandler>();
            Container.Register<ICommandHandler<Maxleaf.EDI.API.Contract.Admin.Commands.ResetPasswordCommand>, ResetPasswordCommandHandler>();

            Container.Register<ICommandHandler<Maxleaf.EDI.API.Contract.Admin.Commands.SetDBAccessCommand>, SetDBAccessCommandHandler>();

            Container.Register<ICommandHandler<Maxleaf.EDI.API.Contract.Client.Commands.ResetPasswordCommand>, ResetCurrentUserPasswordCommandHandler>();
        }
    }
}
